package com.ym.controller.patient.askDoctor;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.common.enums.UserUserTypeEnum;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.entity.InformationEntity;
import com.ym.medical.domain.entity.PatientDetailEntity;
import com.ym.medical.domain.param.CounsellingRoomParam;
import com.ym.medical.domain.param.ExamDoctorParam;
import com.ym.medical.domain.param.InformationParam;
import com.ym.medical.domain.param.PatientDetailParam;
import com.ym.medical.domain.vo.CounsellingRoomRecordVO;
import com.ym.medical.domain.vo.IndexDoctorsVO;
import com.ym.medical.domain.vo.InformationGroupByCountVO;
import com.ym.medical.domain.vo.InformationVo;
import com.ym.medical.domain.vo.ResultDoctor;
import com.ym.medical.enums.InformationInforTypeEnum;
import com.ym.medical.service.ICounsellingRoomSV;
import com.ym.medical.service.IInformationSV;
import com.ym.medical.service.IPatientDetailSV;
import com.ym.medical.service.IResultDoctorSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * Author: zhangfengzhou
 * Date: 2019-03-29
 * Time: 9:12
 */

@Api(tags = "患者端-首页-患者列表")
@RestController
@ApiSign
@RequestMapping(value = "/patient/ask/index",headers= Constants.Api.VERSION)
public class IndexController extends BaseController {
	private static final Logger LOGGER = LoggerFactory.getLogger(IndexController.class);

	@Autowired
	private ICounsellingRoomSV counsellingRoomService;

	@Autowired
	private IPatientDetailSV patientDetailService;

	@Autowired
	private IResultDoctorSV resultDoctorService;

	@Autowired
	private IInformationSV informationSV;


	@ApiOperation("查询当前登陆用户的患者列表")
	@ApiResponses({
			@ApiResponse(code = 200, message = "请求成功", response = PatientDetailEntity.class),
	})
	@GetMapping(value = "/list")
	public APIResponse<PageInfo<PatientDetailEntity>> list(PatientDetailParam model, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "5") int pageSize) {
		model.setUserId(ShiroUtil.getLoginId());
		PageInfo<PatientDetailEntity> list = patientDetailService.listPagePatient(model, pageNum, pageSize);
		return successData(list);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("查询患者的问诊记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "model", value = "问诊记录条件筛选", required = false, paramType = "query", dataType = "CounsellingRoomParam"),
	})
	@ApiResponses({
			@ApiResponse(code = 200, message = "请求成功", response = CounsellingRoomRecordVO.class),
	})
	@GetMapping(value = "/inquiry/record/list")
	public APIResponse<PageInfo<CounsellingRoomRecordVO>> recordList(CounsellingRoomParam model,
																	 @RequestParam(defaultValue = "1") int pageNum,
																	 @RequestParam(defaultValue = "5") int pageSize) {
		model.setUserId(ShiroUtil.getLoginId());
		//model.setIoStates( InquiryOrderStateEnum.WAITING_LIST.getKey() + "," + InquiryOrderStateEnum.COMPLETE.getKey() + "," + InquiryOrderStateEnum.HAVE_ORDER.getKey());
		//model.setFlowStatus(OrderFlowRecordSatusEnum.HAVE_BEEN_ABANDONED.getKey());
		PageInfo<CounsellingRoomRecordVO> counsellingRoomPageInfo = counsellingRoomService.listPagePatientVisitRecord(model, pageNum, pageSize);
		return successData(counsellingRoomPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("患者首页搜索医生")
	@ApiResponses({
			@ApiResponse(code = 200, message = "请求成功", response = ResultDoctor.class),
	})
	@GetMapping(value = "/search/doctor")
	public APIResponse<IndexDoctorsVO> searchDoctor(ExamDoctorParam examDoctor,
													@RequestParam(defaultValue = "1") int pageNum,
													@RequestParam(defaultValue = "5") int pageSize) {
		examDoctor.setCurrentId(ShiroUtil.getLoginId());
		IndexDoctorsVO indexDoctorsVOs = resultDoctorService.searchDoctor(examDoctor, pageSize, pageNum);
		return successData(indexDoctorsVOs);
	}

	@ApiOperation("患者首页问诊话题消息(未读消息)统计")
	@GetMapping(value = "/information/count")
	public APIResponse countAllNoReadInformation() {
		InformationParam informationModel = new InformationParam();
		APIResponse<InformationGroupByCountVO> informationGroupByCountVO = countNoReadInformation(informationModel);
		int count = 0;
		InformationGroupByCountVO data = (InformationGroupByCountVO) informationGroupByCountVO.getData();
		if (data != null) {
			InformationVo inquiryInformation = data.getInquiryInformation();
			InformationVo topicInformation = data.getTopicInformation();
			if (inquiryInformation != null && inquiryInformation.getInforCount() > 0) {
				count += inquiryInformation.getInforCount();
			}
			if (topicInformation != null && topicInformation.getInforCount() > 0) {
				count += topicInformation.getInforCount();
			}
		}
		return successData(count);
	}

	@ApiOperation("患者首页消息类别(未读消息)分组统计")
	@GetMapping(value = "/information/noRead/count")
	public APIResponse<InformationGroupByCountVO> countNoReadInformation(InformationParam informationModel) {
		InformationGroupByCountVO informationGroupByCountVO = new InformationGroupByCountVO();
		informationModel.setReceiverObjId(ShiroUtil.getLoginId());
		List<InformationVo> informationCount = informationSV.countNoReadInformation(informationModel);
		for (InformationVo model : informationCount) {
			if (InformationInforTypeEnum.SYSTEM.getKey().equals(model.getInforType())) {
				informationGroupByCountVO.setSysTemInformation(model);
			}
			if (InformationInforTypeEnum.ACTIVITY.getKey().equals(model.getInforType())) {
				informationGroupByCountVO.setActiveInformation(model);
			}
			if (InformationInforTypeEnum.INQUIRY.getKey().equals(model.getInforType())) {
				informationGroupByCountVO.setInquiryInformation(model);
			}
			if (InformationInforTypeEnum.TOPIC.getKey().equals(model.getInforType())) {
				informationGroupByCountVO.setTopicInformation(model);
			}
			if (InformationInforTypeEnum.PAY.getKey().equals(model.getInforType())) {
				informationGroupByCountVO.setPayInformation(model);
			}
			if (InformationInforTypeEnum.REFUND.getKey().equals(model.getInforType())) {
				informationGroupByCountVO.setRefundInformation(model);
			}
			if (InformationInforTypeEnum.FOLLOW_UP.getKey().equals(model.getInforType())) {
				informationGroupByCountVO.setFollowUpInformation(model);
			}
		}
		return successData(informationGroupByCountVO);
	}

	@ApiOperation("患者首页消息列表")
	@GetMapping(value = "/information/list")
	public APIResponse<Map<String, Object>> listInformation(InformationParam informationModel,
															@RequestParam(defaultValue = "1") int pageNum,
															@RequestParam(defaultValue = "5") int pageSize) {
		informationModel.setReceiverObjId(ShiroUtil.getLoginId());
		informationModel.setDestination(UserUserTypeEnum.PATIENT.getKey());
		Map<String, Object> informationPageInfo = informationSV.listPageInformation(informationModel, pageNum, pageSize);
		return successData(informationPageInfo);
	}

	@ApiOperation("患者首页修改消息")
	@PostMapping(value = "/information/update")
	public APIResponse updateInformation(@RequestBody  InformationEntity information) {
		int flag = informationSV.updateById(information);
		if (flag == 0) {
			return failMsg("患者首页修改消息失败");
		}
		return successData(information);
	}
}
